var prefix = "/sysContractTemplate";

layui.use(['form', 'xmSelect','upload'], function(){
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;
    var xmSelect = layui.xmSelect;
    var loading;



    //自定义验证规则
    form.verify({
        pdfPath:function (value){
            if( !value ){
                return '请上传合同文件';
            }
        },
        name: function(value){
            if( !value ){
                return '请输入合同名称';
            }
        },
        type:function (value){
            if( !value ){
                return '请选择您需要的类型';
            }
        },
        version:function (value){
            if( !value ){
                return '请填写您的版本号';
            }
        }
    });

    upload.render({
        elem: '#contract'
        ,url: '/common/sysFile/upload'
        ,accept: 'file'
        ,before: function(){
            loading = layer.load({
                type:3,
                content:'加载中...',
            });//在post之前定义loading
        }
        ,done: function(res){
            layer.close(loading);
            if( res.code == 0 ){
                layer.msg('上传成功');
                $('.upload').removeClass('layui-hide');
                // $('.upImg').attr('src', res.data);
                $('.upImg').attr('src', '../../../img/pdf.png');
                $("input[name='pdfPath']").val(res.data)
                $("#preview").removeClass("layui-hide").attr("href",res.data);
            }else{
                layer.msg('上传失败,请检查');
            }

            $(".closeUpload").click(function () {

                $(this).parents('.upload').addClass('layui-hide');
                $(".upImg").attr("src",'');
                $("input[name='pdfpath']").val('');
                $("#preview").addClass("layui-hide");

            })
        }
    })

    /**
     * 类型选择器多选
     */
    var ctState = xmSelect.render({
        el: '#contractState',
        name:'projectId',
        data: [],
        max:1,
        maxMethod(seles, item) {
            layer.msg('最大只能选择1个')
            //myTabId
        },
    })

    /**
     * 选择器参数请求
     */
    AjaxGet('/userAunt/seleProject').then(function (response){
        let str = [];
        response.data.map(function (v){
            str.push({
                name:v.name,
                value:v.id
            })
        });
        ctState.update({
            data: str,
            autoRow: true,
        })

    }).catch(function (error){
        layer.msg(error.msg)
    })

    //提交事件
    form.on('submit(submit)', function(data){
        delete data.field.file;
        $.ajax({
            url : prefix+"/insertSysContract",
            type : "post",
            contentType: "application/json",
            data : 	JSON.stringify(data.field),
            success : function(r) {
                if (r.code==0) {
                    layer.msg(r.msg);
                    parent.layer.msg("操作成功");
                    parent.load();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                }else{
                    layer.msg(r.msg);
                }
            }
        });

        return false;
    });




});